package com.telit.android.strategy.context;

import com.telit.android.strategy.SmsCodeStrategyInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
public class SmsCodeStrategyContext {

    private final Map<String, SmsCodeStrategyInterface> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    public SmsCodeStrategyContext(Map<String, SmsCodeStrategyInterface> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach(this.strategyMap::put);
    }

    /**
     * 发送短信
     * @param beanName
     * @param phone
     * @throws Exception
     */
    public String sendSmsCode(String beanName,String phone,String code) throws Exception {
        if (StringUtils.isNotBlank(beanName)) {
            return strategyMap.get(beanName).sendSms(phone,code);
        }
        return null;
    }

    /**
     * 校验code
     * @param beanName
     * @param phone
     * @param code
     */
    public void checkCode(String beanName,String phone,String code,Long userId){
        if (StringUtils.isNotBlank(beanName)) {
            strategyMap.get(beanName).checkCode(phone,code,userId);
        }
    }

}
